package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.util.WebUtils;

public class LoginFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletrequest;
		HttpServletResponse response = (HttpServletResponse) servletresponse;
		String isLogin = (String) WebUtils.getSessionAttribute(request, "username");
		boolean logindo = request.getRequestURL().toString().contains("userlogin.do");
		boolean loginpage = request.getRequestURL().toString().contains("login.html");
		if (isLogin != null||logindo||loginpage) {
			filterchain.doFilter(request, response);
			return;
		}else{
			//request.getContextPath();
			response.sendRedirect(request.getContextPath()+"/login.html");
		}
	}

	@Override
	public void init(FilterConfig filterconfig) throws ServletException {

	}

}
